package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class UserPaidTrackServiceImpl extends ServiceImpl<UserPaidTrackMapper, UserPaidTrack> implements UserPaidTrackService {

	@Autowired
	private UserPaidAlbumMapper userPaidAlbumMapper;


	@Override
	public Map<String, Integer> getUserIsPaidTrack(Long userId, Long albumId, List<Long> needCheckTrackIdList) {
		Long count = userPaidAlbumMapper.selectCount(new LambdaQueryWrapper<UserPaidAlbum>()
				.eq(UserPaidAlbum::getUserId, userId)
				.eq(UserPaidAlbum::getAlbumId, albumId));
		HashMap<String, Integer> map = new HashMap<>();
		if(count > 0){
			for (Long trackId : needCheckTrackIdList) {
				//不需要购买
				map.put(trackId.toString(),1);
			}
			return map;
		}
		for (Long trackId : needCheckTrackIdList) {
			//需要购买
			map.put(trackId.toString(),0);
		}
		List<UserPaidTrack> list = this.lambdaQuery().eq(UserPaidTrack::getUserId, userId).in(UserPaidTrack::getTrackId, needCheckTrackIdList).list();
		if(!CollectionUtils.isEmpty(list)){
			Set<Long> trackIds = list.stream().map(UserPaidTrack::getTrackId).collect(Collectors.toSet());
			for (Long trackId : trackIds) {
				//不需要购买
				map.put(trackId.toString(),1);
			}
		}
		return map;
	}
}
